<template>
  <view class="goods-item">
    <!-- 左侧图片 -->
    <view class="goods-item-left">
      <radio :checked="item.goods_state" color="#c00000" v-if="showRadio" @click="radioClickHandeler"></radio>
      <image :src="item.goods_small_logo || defaultPic" class="goods-pic" mode=""></image>
    </view>
    <!-- 右侧内容 -->
    <view class="goods-item-right">
      <view class="goods-name">{{item.goods_name}} </view>
      <view class="goods-info-box">
        <view class="goods-price">￥{{item.goods_price | toFixed }} </view> 
        <uni-number-box :min="1" :value="item.goods_count" v-if="showNum" @change="numChangeHandler"></uni-number-box>
        </view>
      </view>
    </view>
</template>

<script>
  export default {
    name:"my-goods",
    props:{
      item:{
        type:Object,
        default:{}
      },
      showRadio:{
        type:Boolean,
        default:false // 表示默认不会展示radio组件
      },
      showNum:{
        type:Boolean,
        default:false
      }
    },
    data() {
      return {
        // 默认图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      }
    },
    methods:{
      radioClickHandeler(){ // 这是radio的点击事件处理函数
        this.$emit('radio-change',{
          goods_id:this.item.goods_id,
          goods_state: !this.item.goods_state
        })
      },
      // NumberBox 组件的 change 事件处理函数
        numChangeHandler(val) {
          // 通过 this.$emit() 触发外界通过 @ 绑定的 num-change 事件
          this.$emit('num-change', {
            // 商品的 Id
            goods_id: this.item.goods_id,
            // 商品的最新数量
            goods_count: +val
          })
        }
    },
    filters:{
      toFixed(num){ // 过滤两位小数
        return Number(num).toFixed(2)
      }
    }
  }
</script>

<style lang="scss">
.goods-item{
  // width: 750px;
  // box-sizing: border-box;
  display: flex;
  padding: 10px 5px ;
  border-bottom: 1px solid #F0F0F0;
  .goods-item-left{
    margin-right: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center; 
    .goods-pic{
      width: 100px;
      height: 100px;
      display: block;
    }
  }
  .goods-item-right{
    display: flex;
    flex: 1;
    flex-direction: column;
    justify-content: space-between;
    .goods-name{
      font-size: 13px;
    }
    .goods-info-box{
      display: flex;
      justify-content: space-between;
      align-items: center;
      .goods-price{
        color: #c00000;
        font-size: 16px;
      }
    }
  }
}
</style>
